﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Flatland.Elements {
    /// <summary>
    /// Panel class
    /// </summary>
    public class PanelController {
        /// <summary>
        /// Position of the Panel
        /// </summary>
        public Vector2 Position { get; set; }

        /// <summary>
        /// Get or set that the panel is hiding
        /// </summary>
        public bool Hiding { get; set; }

        /// <summary>
        /// Get or set the texture of the panel
        /// </summary>
        public Texture2D PanelTexture { get; set; }

        /// <summary>
        /// Get or set the panel height.
        /// </summary>
        public int PanelHeight { get; set; }

        /// <summary>
        /// Sprite bact for drawing the panel that ignores the Camera.
        /// </summary>
        public SpriteBatch Panelbatch { get; set; }

        /// <summary>
        /// Get or set Agent being displayed.
        /// </summary>
        public Agent Agent { get; set; }

        /// <summary>
        /// Private. Rotation radian for the Agent on the display
        /// </summary>
        private float _profileRotation;

        /// <summary>
        /// True if the panel will display info about Flatland and key bindings.
        /// </summary>
        private bool showInfo;

        /// <summary>
        /// Spritefont for the text on the panel.
        /// </summary>
        private SpriteFont _font;

        /// <summary>
        /// Get or set a system output string that will be displayed at the bottom of the panel.
        /// </summary>
        public string news_output { get; set; }


        public void LoadContent() {
            PanelHeight = 200;
            int width = Program.FlatlandWorld.GraphicsDeviceManager.PreferredBackBufferWidth;
            int height = Program.FlatlandWorld.GraphicsDeviceManager.PreferredBackBufferHeight;
            Color[] panelcolor = new Color[width * PanelHeight];
            for (int i = 0; i < width * PanelHeight; i++) {
                panelcolor[i] = new Color(0, 0, 0, 180);
            }
            PanelTexture = new Texture2D(Program.FlatlandWorld.GraphicsDevice,
                                         width,
                                         PanelHeight,
                                         false,
                                         SurfaceFormat.Color);
            PanelTexture.SetData(panelcolor);
            Position = new Vector2(0, height);
            Panelbatch = new SpriteBatch(Program.FlatlandWorld.GraphicsDevice);
            Hiding = true;
            _font = Program.FlatlandWorld.Content.Load<SpriteFont>("Courier_new");
        }

        private float PanelYonToggle;
        private int time;
        public void Show(Agent agent) {
            news_output = string.Empty;
            Agent = agent;
            Console.WriteLine("Agent verts: " + agent.Vertices());
            PanelYonToggle = Position.Y;
            time = 0;
            Hiding = false;
        }

        public void Hide() {
            Agent = null;
            PanelYonToggle = Position.Y;
            time = 0;
            Hiding = true;
            showInfo = false;
        }

        public void ShowInfo() {
            //Make sure agent is null so draw will display the info.
            Agent = null;
            showInfo = !showInfo;
            Hiding = false;
        }



        public void Update() {
            _profileRotation += 0.01f;
            if (Hiding) {// && Program.FlatlandWorld.GraphicsDeviceManager.PreferredBackBufferHeight <= Position.Y) {
                int end = 9999;

                Position = new Vector2(Position.X, end);
            }
            else if (!Hiding) {// && Program.FlatlandWorld.GraphicsDeviceManager.PreferredBackBufferHeight - PanelTexture.Height <= Position.Y) {

                int end = Program.FlatlandWorld.GraphicsDeviceManager.PreferredBackBufferHeight - PanelTexture.Height;
                Position = new Vector2(Position.X, end);
            }
        }



        public void Draw() {
            Panelbatch.Begin();
            Panelbatch.Draw(PanelTexture,
                           Position,
                           null,
                           Color.White,
                           0f,
                           Vector2.Zero,
                           1f,
                           SpriteEffects.None, 0f);

            if (Agent != null) {
                //Agent profile
                float profileX = 100;
                float profileY = Position.Y + PanelTexture.Height / 2;
                Panelbatch.Draw(
                    Agent.Texture,
                    new Vector2(profileX, profileY),
                    null,
                    Color.White,
                    _profileRotation,
                    new Vector2(Agent.Texture.Width / 2, Agent.Texture.Height / 2),
                    1f,
                    SpriteEffects.None,
                    0f);

                //Agent stats
                int textIndent = 220;
                int top = 10;
                int fontSize = 20;
               // string state = "State: " + Agent.Layer;
                string sides = "Sides: " + (Agent.IsPerfectCircle ? "Perfect Circle!" : Agent.IsWoman ? 2 + "" : Agent.Vertices() + "");
                string inches = ((Agent.Size*10)/75).ToString(); //Todo clean this
                int dot = inches.IndexOf(',');
                inches = inches.Substring(0, dot + 2);

                string size = "Size: " + inches + "\"";
                string output = "Says: " + Agent.Output;
                string id = "Flatlander: " + Agent.Id;
                string birth = "Birth: " + Agent.BirthDay.ToShortDateString() + "-" + Agent.BirthDay.ToShortTimeString();
                string age = "Age: " + DateTime.Now.Subtract(Agent.BirthDay);
                string food = "Food eaten: " + Agent.FoodEaten;

                //2 column
                string print = "Press 'g' to print genes.";

                //Line at bottom of panel to print system wide output info set on the output variable
                string news = string.IsNullOrEmpty(news_output) ? "" : "News: " + news_output;

                //1 Column
               // Panelbatch.DrawString(_font, state, new Vector2(textIndent, Position.Y + top), Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
                Panelbatch.DrawString(_font, output, new Vector2(textIndent, Position.Y + top + fontSize), Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
                Panelbatch.DrawString(_font, sides, new Vector2(textIndent, Position.Y + top + fontSize * 2), Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
                Panelbatch.DrawString(_font, size, new Vector2(textIndent, Position.Y + top + fontSize * 3), Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
                Panelbatch.DrawString(_font, id, new Vector2(textIndent, Position.Y + top + fontSize * 4), Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
                Panelbatch.DrawString(_font, birth, new Vector2(textIndent, Position.Y + top + fontSize * 5), Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
                Panelbatch.DrawString(_font, age, new Vector2(textIndent, Position.Y + top + fontSize * 6), Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
                Panelbatch.DrawString(_font, food, new Vector2(textIndent, Position.Y + top + fontSize * 7), Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
                Panelbatch.DrawString(_font, news, new Vector2(textIndent, Position.Y + top + fontSize * 8), Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);

                //2 column
                Panelbatch.DrawString(_font, print, new Vector2(textIndent * 3, Position.Y + top + fontSize * 7), Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
            }

            /*
             * No agent, so we display info.
             */
            else if(showInfo){
                int textIndent = 220;
                int top = 10;
                int fontHeight = 20;

                //1 Column
                string pop = "Population M:" + Program.FlatlandWorld.Flatlanders.Count + " W: " + Program.FlatlandWorld.Flatlanderines.Count;
                string food = "Food reserve: " + Program.FlatlandWorld.FoodReserve.Count;
                string allTime = "All time Flatlanders: " + FlatlandWorld.TotalAgentsSpawned;

                //Line at bottom of panel to print system wide output info set on the output variable
                string systemOutputline = string.IsNullOrEmpty(news_output) ? "" : "News: " + news_output;

                //2 column
                string cam = "Z/X Zoom in/out: ";
               // string speed = "1-9 Increase simulation speed: ";
                string move = "Use arrow keys to navigate around.";
                string mouse = "Left click Flatlander to select";
                string print = "\'P\'  Print selected Flatlander";
                string feed = "\'F\' Throw a random amount of food.";
                string add = "\'B\' Breed a new Flatlander";
                string col = "\'C\' Change world color";
               // string help = "\'H\' To toggle this panel";
                string quit = "\'Q\' To quit";

                //1 column
                Panelbatch.DrawString(_font, pop, new Vector2(textIndent, Position.Y + top), Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
                Panelbatch.DrawString(_font, food, new Vector2(textIndent, Position.Y + top + fontHeight), Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
                Panelbatch.DrawString(_font, allTime, new Vector2(textIndent, Position.Y + top + fontHeight * 2), Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
                Panelbatch.DrawString(_font, systemOutputline, new Vector2(textIndent, Position.Y + top + fontHeight * 8), Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
               
                //2 Column
                int textIndent2 = textIndent*3;
                Panelbatch.DrawString(_font, cam, new Vector2(textIndent2, Position.Y + top), Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
              //  Panelbatch.DrawString(_font, speed, new Vector2(textIndent2, Position.Y + top + fontHeight), Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
                Panelbatch.DrawString(_font, move, new Vector2(textIndent2, Position.Y + top + fontHeight * 1), Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
                Panelbatch.DrawString(_font, mouse, new Vector2(textIndent2, Position.Y + top + fontHeight * 2), Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
                Panelbatch.DrawString(_font, print, new Vector2(textIndent2, Position.Y + top + fontHeight * 3), Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
                Panelbatch.DrawString(_font, feed, new Vector2(textIndent2, Position.Y + top + fontHeight * 4), Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
                Panelbatch.DrawString(_font, add, new Vector2(textIndent2, Position.Y + top + fontHeight * 5), Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
                Panelbatch.DrawString(_font, col, new Vector2(textIndent2, Position.Y + top + fontHeight * 6), Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);                
                Panelbatch.DrawString(_font, quit, new Vector2(textIndent2, Position.Y + top + fontHeight * 7), Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);

            }
            Panelbatch.End();
        }
    }
}
